<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use Illuminate\Validation\Rule;

class DeviceConfigController extends Controller
{

    use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\DeviceConfig::class);
    }

    protected function getAttributes(){
        return [
            'Identification' => '标识',
            'device_position' => '设备位置',
            'device_desc' => '设备描述',
            'device_ip' => '设备IP地址',
            'device_login' => '设备登录账户',
            'device_password' => '设备登录密码',
            'device_serial_port' => '连接串口',
            'device_port' => '串口端口号',
            'device_baud_rate' => '串口波特率',
            'remark' => '备注'
        ];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return formatJsonResponse($this->pageModel(request()->input()));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return formatJsonResponse($this->findModel($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rules = [
            'Identification' => 'required',
            'device_position' => 'required',
            'device_desc' => 'required',
            'device_ip' => 'nullable|ip'
        ];

        $this->validate($rules);

        $this->updateModel($id,$request->all());

        return formatJsonResponse();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
